package com.jinlia.show.modules.base.system.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinlia.show.modules.base.model.query.FreightChargesQuery;
import com.jinlia.show.modules.base.model.vo.FreightChargesAdapterVO;
import com.jinlia.show.modules.base.system.entity.FreightChargesAdapter;
import com.jinlia.show.modules.base.system.entity.Goods;
import com.jinlia.show.modules.base.system.mapper.FreightChargesAdapterMapper;
import com.jinlia.show.modules.base.wxapp.entity.ShoppingCart;
import com.jinlia.show.modules.base.wxapp.service.ShoppingCartMapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class FreightChargesAdapterMapperService extends ServiceImpl<FreightChargesAdapterMapper, FreightChargesAdapter> {

    @Autowired
    private ShoppingCartMapperService shoppingCartMapperService;
    @Autowired
    private GoodsMapperService goodsMapperService;

    public FreightChargesAdapterVO calculateMoney(FreightChargesQuery queryParams) {
        List<FreightChargesAdapter> list = this.lambdaQuery()
                .orderByAsc(FreightChargesAdapter::getDistance)
                .list();
        FreightChargesAdapter fitFreightChargesAdapter = null;
        for (FreightChargesAdapter freightChargesAdapter : list) {
            if (queryParams.getDistance() <= freightChargesAdapter.getDistance()) {
                fitFreightChargesAdapter = freightChargesAdapter;
                break;
            }
        }
        //重量合计
        BigDecimal sumWeight = BigDecimal.ZERO;
        if (!CollectionUtils.isEmpty(queryParams.getShoppingCardIds())) {
            //购车id
            List<ShoppingCart> listShoppingCart = shoppingCartMapperService.lambdaQuery()
                    .in(ShoppingCart::getId, queryParams.getShoppingCardIds())
                    .list();
            List<Long> collect = listShoppingCart.stream().map(ShoppingCart::getGoodsId).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(collect)) {
                Map<Long, Integer> mapGoods = listShoppingCart.stream().
                        collect(Collectors.toMap(ShoppingCart::getGoodsId, ShoppingCart::getNum, Integer::sum));
                List<Goods> listGoods = goodsMapperService.lambdaQuery()
                        .in(Goods::getId, collect)
                        .list();
                for (Goods goods : listGoods) {
                    Integer num = mapGoods.get(goods.getId());
                    BigDecimal weight = Objects.isNull(goods.getWeight()) ? BigDecimal.valueOf(1) : BigDecimal.valueOf(goods.getWeight());
                    sumWeight = sumWeight.add(weight).multiply(new BigDecimal(num));
                }
            }

        }
        FreightChargesAdapterVO freightChargesAdapterVO = new FreightChargesAdapterVO();
        freightChargesAdapterVO.setWeight(sumWeight);//合计重量
        freightChargesAdapterVO.setDistance(queryParams.getDistance());// 距离
        freightChargesAdapterVO.setUnitPrice(fitFreightChargesAdapter.getUnitPrice());
        freightChargesAdapterVO.setSumMoney(
                fitFreightChargesAdapter.getUnitPrice() //单价
                        .multiply(sumWeight) //重量
                        .multiply(BigDecimal.valueOf(queryParams.getDistance()))); //距离
        return freightChargesAdapterVO;
    }
}
